package com.rpframework.website.device.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.rpframework.core.BaseService;
import com.rpframework.utils.NumberUtils;
import com.rpframework.utils.Pager;
import com.rpframework.website.device.dao.IInteractDao;
import com.rpframework.website.device.domain.Interact;
import com.rpframework.website.device.util.HConstants;
/**
 * 互动接口类
 * @author zhangli
 * @date 2016年3月17日 上午10:14:34
 */
@Service
public class InteractService extends BaseService{
	
	@Resource public IInteractDao iDao;
	
	public Pager<Interact> getpager(Pager<Interact> pager) {
		long startTime=System.currentTimeMillis();
		List<Interact>  list=iDao.doPager(this.packageMyBatisParam(pager));
		pager.setItemList(list);
		pager.setCostTime(System.currentTimeMillis() - startTime);
		return pager;
	}

	public boolean doSaveOrUpdate(Interact t) {
		if(t == null) {
			throw new IllegalArgumentException("非法参数!");
		}
		if(NumberUtils.isValid(t.getId())) {
			Interact DBUtils = iDao.select(t.getId());
			Assert.notNull(DBUtils, "更新时找不到实体 " + t.getId());
			if(t.getUrlList()==null){
				t.setUrlList(DBUtils.getUrlList());
			}
			t.setModifyTime(System.currentTimeMillis() / 1000);
			return iDao.update(t);
		}
		t.setCreateTime(System.currentTimeMillis() / 1000);
		return iDao.insert(t);
	}

	public boolean doUpdateState(Integer id) {
		if(NumberUtils.isValid(id)){
			Interact t = iDao.select(id);
			if(t.getState() == HConstants.EntityState.STATE_NORMAL){
				t.setState(HConstants.EntityState.STATE_DISABLE);
			}else{
				t.setState(HConstants.EntityState.STATE_NORMAL);
			}
			t.setModifyTime(System.currentTimeMillis()/1000);
			return iDao.update(t);
		}
		return false;
	}

	public Pager<Interact> listLikePager(Pager<Interact> pager) {
		long startTime=System.currentTimeMillis();
		List<Interact>  list=iDao.listLikePager(this.packageMyBatisParam(pager));
		pager.setItemList(list);
		pager.setCostTime(System.currentTimeMillis() - startTime);
		return pager;
	}

}
